package com.byx.chatclient.view.activity;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.byx.chatclient.ChatApp;
import com.byx.chatclient.R;
import com.byx.chatclient.entity.Constant;
import com.byx.chatclient.entity.Friend;
import com.byx.chatclient.entity.ResultCallback;
import com.byx.chatclient.entity.UserPo;
import com.byx.chatclient.factory.RetrofitFactory;
import com.byx.chatclient.http.ActionRequest;
import com.byx.chatclient.utils.GradientStatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * @author Superb
 */
@SuppressLint("NonConstantResourceId")
public class FriendPerferenceActivity extends AppCompatActivity {
    @BindView(R.id.tv_friend_remark)
    protected TextView mTvRemark;
    @BindView(R.id.tv_friend_group)
    protected TextView mTvGroup;

    private Unbinder unbinder;
    private Friend friend;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friend_perference);
        GradientStatusBarUtil.getInstance().bind(this).withBackgroundResource(R.drawable.toolbar).init();
        unbinder = ButterKnife.bind(this);
        Intent intent = getIntent();
        friend = (Friend) intent.getSerializableExtra(Constant.INTENT_KEY_FRIEND);
        assert friend != null;
        mTvRemark.setText(friend.getFriendRemarkName());
        mTvGroup.setText(friend.getGroupName());
    }

    @OnClick({R.id.rl_remark,R.id.rl_group,R.id.btn_delete_friend})
    protected void onClick(View view){
        switch (view.getId()){
            case R.id.rl_remark:
                View viewRemark = LayoutInflater.from(this).inflate(R.layout.layout_change_person_info_text_dialog,null);
                ((TextView) viewRemark.findViewById(R.id.tv_dialog_title)).setText("修改备注");
                Dialog dialog = new AlertDialog.Builder(this).setView(viewRemark).create();
                dialog.show();

                viewRemark.findViewById(R.id.tv_dialog_enter).setOnClickListener(v -> {
                    String data = ((EditText) viewRemark.findViewById(R.id.et_dialog_input)).getText().toString();
                    Toast.makeText(FriendPerferenceActivity.this, data, Toast.LENGTH_SHORT).show();
                    RetrofitFactory.getBean(ActionRequest.class).changeFriendRemark(ChatApp.getInstance().getUserPo().getId(),friend.getId(),data)
                            .enqueue(new ResultCallback<Void>() {
                                @Override
                                public void onResultSuccess(Void v) {
                                    Toast.makeText(FriendPerferenceActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
                                    dialog.dismiss();
                                    mTvRemark.setText(data);
                                }

                                @Override
                                public void onResultFailure(int code, String msg) {

                                }
                            });
                });
                break;
            case R.id.rl_group:
                Toast.makeText(this, "暂不支持修改分组，等待开发者增加此功能", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_delete_friend:
                RetrofitFactory.getBean(ActionRequest.class).deleteFriend(ChatApp.getInstance().getUserPo().getId(),friend.getId())
                        .enqueue(new ResultCallback<Void>() {
                            @Override
                            public void onResultSuccess(Void data) {
                                Toast.makeText(FriendPerferenceActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                finish();
                            }

                            @Override
                            public void onResultFailure(int code, String msg) {

                            }
                        });
                break;
            default:break;
        }
    }

    @Override
    protected void onDestroy() {
        GradientStatusBarUtil.getInstance().unbind();
        unbinder.unbind();
        super.onDestroy();
    }
}